package com.fafu.common.web.ex;

import com.fafu.common.web.bean.Result;
import com.fafu.common.web.bean.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局统一异常处理器
 *
 * @author hc
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = {GlobalException.class, RuntimeException.class, Exception.class})
    public Result exceptionHandler(GlobalException ex) {
        final Result result = ResultUtil.error(ex.getCode(), ex.getMsg())
                .addData("aim", ex.getAim());
        if (ex.getMessage() == null) {
            log.info(ex.getMessage());
        } else {
            log.error(ex.getMessage());
        }
        return result;
    }

}
